package com.tms.admin.controller;

import com.tms.admin.entity.TmsAdmin;
import com.tms.admin.entity.vo.LoginVO;
import com.tms.admin.service.TmsAdminService;
import com.tms.common.entity.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/*-----------------------------------
 *   @Time: 2022/9/5 15:00
 *   @Description: 登录
-----------------------------------*/
@Api(value = "登录管理") // Swagger
@RestController
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {
    @Autowired
    private TmsAdminService tmsAdminService;

    @ApiOperation(value = "登录操作")
    @PostMapping("login")
    public ResultVO login(@RequestBody LoginVO loginVO) {
        String token = tmsAdminService.login(loginVO);
        return ResultVO.ok().data("token", token);
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("logout")
    public ResultVO logout(@RequestHeader("X-Token") String token) { 
        tmsAdminService.logout(token);
        return ResultVO.ok();
    }
    @ApiOperation(value = "根据 token 获取用户信息")
    @PostMapping("token")
    public ResultVO login(@RequestBody(required = false) String token) {
        TmsAdmin user = tmsAdminService.getByToken(token);
        return ResultVO.ok().data("item", user);
    }
}
